/**
 * 二叉树-最大深度（后序遍历、深度优先、递归）-力扣104
 */
public class Practice_28 {
    public static void main(String[] args) {

        TreeNode head = new TreeNode(1);

        head.left = new TreeNode(2);

        head.right = new TreeNode(3);

        // head.left.left = new TreeNode(4);

        // head.left.right = new TreeNode(5);

        head.right.left = new TreeNode(6);

        head.right.right = new TreeNode(7);


        int height = maxDepth(head);

    }

    public static int maxDepth(TreeNode root) {


        return getHeight(root);

    }

    //求最大深度，使用算最大深度的方法：后序遍历（左右中）
    public static int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }//先判断空，为空返回0

        int leftHeight = getHeight(root.left);//左侧遍历
        int rightHeight = getHeight(root.right);//右侧遍历

        if (leftHeight >= rightHeight) {
            return 1 + leftHeight;
        } else {
            return 1 + rightHeight;
        }
        //  return leftHeight > rightHeight ? (1 + leftHeight) : (1 + rightHeight);


    }


    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

}
